/// <reference path="jquery-1.5.1-vsdoc.js" />


function searchGroup(number)
{
    var startRow = 0;
    var endRow = 0;
    var rows = 0;
    var currentPage = 0;
    var pages = 0;
    var strResult = "";
    var name = $("#nameFilter").val();
    var max = $("#max").val();
    var start = number;
    //var start = 0;

    var xmlhttp = new XMLHttpRequest();

    xmlhttp.open("GET", "/Group/SearchXML?name=" + name + "&start=" + start + "&max=" + max, false);
    xmlhttp.send();

    //if (x[0].getElementsByTagName("Id")[0].childNodes[0].nodeValue != 0)
    if (xmlhttp.responseText != "")
    {
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(xmlhttp.responseText, "text/xml");

        var x = xmlDoc.getElementsByTagName("Group");
        var n = 0;

        //Get Number of rows
        rows = x[0].getElementsByTagName("Rows")[0].childNodes[0].nodeValue;
        //Get Start row.
        startRow = x[0].getElementsByTagName("StartRow")[0].childNodes[0].nodeValue;
        //Get current page
        currentPage = x[0].getElementsByTagName("CurrentPage")[0].childNodes[0].nodeValue;
        //Get pages
        pages = x[0].getElementsByTagName("Pages")[0].childNodes[0].nodeValue;
        //Get End row
        endRow = currentPage * max;
        if (endRow >= rows)
        {
            endRow = rows;
        }
        //Get number of pages
        pages = x[0].getElementsByTagName("Pages")[0].childNodes[0].nodeValue;

       
        strResult += "<div class=\"aui-group count-pagination\">";
        strResult += "        <div class=\"results-count aui-item\">";
        strResult += "            Displaying groups <span class=\"results-count-start\">" + startRow + "</span> to <span>" + endRow + "</span> of <strong class=\"results-count-total\">" + rows + "</strong>.";
        strResult += "        </div>";
        strResult += "    </div>";
        //Change page
        if (pages > 1)
        {
            strResult += ChangePage(currentPage, pages, max);
        }

        strResult += "<table id=\"group_browser_table\" class=\"aui aui-table-rowhover\"> ";
        strResult += "        <thead>";
        strResult += "            <tr>";
        strResult += "                <th width=\"5%\">";
        strResult += "                    #";
        strResult += "                </th>";
        strResult += "                <th width=\"30%\">";
        strResult += "                    Group Name";
        strResult += "                </th>";
        strResult += "                <th width=\"25%\">";
        strResult += "                    Users";
        strResult += "                </th>";
        strResult += "                <th width=\"30%\">";
        strResult += "                    Permission Schemes";
        strResult += "                </th>";           
        strResult += "                    <th width=\"10%\">";
        strResult += "                        Operations";
        strResult += "                    </th>";           
        strResult += "            </tr>";
        strResult += "        </thead>";
        strResult += "	      <tbody>";


        for (i = 0; i < x.length; i++)
        {
            n++;

         strResult += "           <tr>";
         strResult += "               <td>";
         strResult += n;
         strResult += "               </td>";
         strResult += "               <td>";
         strResult += x[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue;
         //strResult += "                   <a href=\"#\">" + x[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue + "</a>";
         strResult += "               </td>";
		 strResult += "               <td>";
		 strResult += "                   <a href=\"#\">" + x[i].getElementsByTagName("NumberOfUser")[0].childNodes[0].nodeValue + "</a>";
         strResult += "              </td>";
         strResult += "               <td>";           
         strResult += "                       &nbsp;";          
         strResult += "               </td>";
         strResult += "               <td>";
         strResult += "                   <ul class=\"operations-list\">";
         strResult += "                       <li><a id=\"del_aq\" href=\"/Group/Delete/" + x[i].getElementsByTagName("Id")[0].childNodes[0].nodeValue + "\">Delete</a></li>";
         strResult += "                       <li><a id=\"edit_members_of_aq\" href=\"/Group/Edit/" + x[i].getElementsByTagName("Id")[0].childNodes[0].nodeValue + "\">Edit Members</a></li>";
         strResult += "                   </ul>";
         strResult += "              </td>";      
         strResult += "           </tr>";
         }
         strResult += "       </tbody>";
         strResult += "</table>";

        //Change page
        if (pages > 1)
        {
            strResult += ChangePage(currentPage, pages, max);
        }
        document.getElementById("searchResult").innerHTML = strResult;
        //document.write(strResult);
    }
    else
    {
        strResult += "<div class=\"aui-group count-pagination\">";
        strResult += "        <div class=\"results-count aui-item\">";
        strResult += "            Displaying groups <span class=\"results-count-start\">" + startRow + "</span> to <span>" + endRow + "</span> of <strong class=\"results-count-total\">" + rows + "</strong>.";
        strResult += "        </div>";
        strResult += "    </div>";
        strResult += "<table id=\"group_browser_table\" class=\"aui aui-table-rowhover\">";
        strResult += "        <thead>";
        strResult += "            <tr>";
        strResult += "                <th width=\"40%\">";
        strResult += "                    Group Name";
        strResult += "                </th>";
        strResult += "                <th width=\"20%\">";
        strResult += "                    Users";
        strResult += "                </th>";
        strResult += "                <th width=\"30%\">";
        strResult += "                    Permission Schemes";
        strResult += "                </th>";
        strResult += "                    <th width=\"10%\">";
        strResult += "                        Operations";
        strResult += "                    </th>";
        strResult += "            </tr>";
        strResult += "        </thead>";
        strResult += "	      <tbody>";
        strResult += "	      </tbody>";
        strResult += "    </table>";
        document.getElementById("searchResult").innerHTML = strResult;
    }

}

function ChangePage(currentPage, pages, max)
{
    var strResult = "";
    
    strResult += "<div class=\"pagination aui-item\" style=\"float:right\">";

    if (Number(currentPage) > 1)
    {
        var previous = (Number(currentPage) - 1) * max - max;
        strResult += "<a class=\"icon icon-previous\" title=\"Previous\" href=\"#\" onclick=\"filterGroup(" + previous + ")\">";
        strResult += "<div>&lt;&lt;</div></a>";
    }
    for (var i = 1; i <= pages; i++)
    {
        if (i == currentPage)
        {
            strResult += "    <strong>" + i + "</strong>";
        }
        else
        {
            var i_start = i * max - max;
            strResult += "    <a href=\"#\" onclick=\"filterGroup(" + i_start + ")\">" + i + "</a>";
        }
    }
    if (Number(currentPage) < Number(pages))
    {
        var next = (Number(currentPage) + 1) * max - max;
        strResult += "    <a class=\"icon icon-next\" title=\"Next\" href=\"#\" onclick=\"filterGroup(" + next + ")\">";
        strResult += "    <div>&gt;&gt;</div>";
        strResult += "    </a>";
    }
    strResult += "</div>";
    return strResult;
}

function getInfo(number)
{
    var name = $("#nameFilter").val();
    var start = number;
    var max = $("#max").val();

    // poor man's validation
    return { Name: name, Start: start, Max: max };
}

function filterGroup(number)
{
    $(function ()
    {
        var startRow = 0;
        var endRow = 0;
        var rows = 0;
        var currentPage = 0;
        var pages = 0;
        var strResult = "";
        var n = 0;
        var info = getInfo(number);

        if (info == null)
        {
            alert("No infomation!");
            return;
        }

        $.post("Group/Filter", info, function (data)
        {
            //alert("Post ok!");
            
            // get the result and do some magic with it
            //Get Number of rows
            rows = data.Rows;
            
            //Get Start row.
            startRow = data.StartRow;
            
            //Get current page
            currentPage = data.CurrentPage;
            //Get pages
            pages = data.Pages;
            //Get End row
            endRow = currentPage * info.Max;
            if (endRow >= rows)
            {
                endRow = rows;
            }
            
            if (rows != 0)
            {
                //alert("Row != 0!");
                strResult += "<div class=\"aui-group count-pagination\">";
                strResult += "        <div class=\"results-count aui-item\">";
                strResult += "            Displaying groups <span class=\"results-count-start\">" + startRow + "</span> to <span>" + endRow + "</span> of <strong class=\"results-count-total\">" + rows + "</strong>.";
                strResult += "        </div>";
                strResult += "    </div>";
                //Change page
                if (pages > 1)
                {
                    strResult += ChangePage(currentPage, pages, info.Max);
                }

                strResult += "<table id=\"group_browser_table\" class=\"aui aui-table-rowhover\"> ";
                strResult += "        <thead>";
                strResult += "            <tr>";
                strResult += "                <th width=\"5%\">";
                strResult += "                    #";
                strResult += "                </th>";
                strResult += "                <th width=\"30%\">";
                strResult += "                    Group Name";
                strResult += "                </th>";
                strResult += "                <th width=\"25%\">";
                strResult += "                    Users";
                strResult += "                </th>";
                strResult += "                <th width=\"30%\">";
                strResult += "                    Permission Schemes";
                strResult += "                </th>";
                strResult += "                    <th width=\"10%\">";
                strResult += "                        Operations";
                strResult += "                    </th>";
                strResult += "            </tr>";
                strResult += "        </thead>";
                strResult += "	      <tbody>";


                for (i = 0; i < data.Groups.length; i++)
                {
                    n++;

                    strResult += "           <tr>";
                    strResult += "               <td>";
                    strResult += n;
                    strResult += "               </td>";
                    strResult += "               <td>";
                    strResult += data.Groups[i].Name;
                    //strResult += "                   <a href=\"#\">" + x[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue + "</a>";
                    strResult += "               </td>";
                    strResult += "               <td>";
                    strResult += "                   <a href=\"#\">" + data.Groups[i].NumberOfUser + "</a>";
                    strResult += "              </td>";
                    strResult += "               <td>";
                    strResult += "                       &nbsp;";
                    strResult += "               </td>";
                    strResult += "               <td>";
                    strResult += "                   <ul class=\"operations-list\">";
                    strResult += "                       <li><a id=\"del_aq\" href=\"/Group/Delete/" + data.Groups[i].Id + "\">Delete</a></li>";
                    strResult += "                       <li><a id=\"edit_members_of_aq\" href=\"/Group/Edit/" + data.Groups[i].Id + "\">Edit Members</a></li>";
                    strResult += "                   </ul>";
                    strResult += "              </td>";
                    strResult += "           </tr>";
                }
                strResult += "       </tbody>";
                strResult += "</table>";

                //Change page
                if (pages > 1)
                {
                    strResult += ChangePage(currentPage, pages, info.Max);
                }
                document.getElementById("searchResult").innerHTML = strResult;
                //document.write(strResult);
            }
            else
            {
                //alert("Row == 0!");
                strResult += "<div class=\"aui-group count-pagination\">";
                strResult += "        <div class=\"results-count aui-item\">";
                strResult += "            Displaying groups <span class=\"results-count-start\">" + startRow + "</span> to <span>" + endRow + "</span> of <strong class=\"results-count-total\">" + rows + "</strong>.";
                strResult += "        </div>";
                strResult += "    </div>";
                strResult += "<table id=\"group_browser_table\" class=\"aui aui-table-rowhover\">";
                strResult += "        <thead>";
                strResult += "            <tr>";
                strResult += "                <th width=\"40%\">";
                strResult += "                    Group Name";
                strResult += "                </th>";
                strResult += "                <th width=\"20%\">";
                strResult += "                    Users";
                strResult += "                </th>";
                strResult += "                <th width=\"30%\">";
                strResult += "                    Permission Schemes";
                strResult += "                </th>";
                strResult += "                    <th width=\"10%\">";
                strResult += "                        Operations";
                strResult += "                    </th>";
                strResult += "            </tr>";
                strResult += "        </thead>";
                strResult += "	      <tbody>";
                strResult += "	      </tbody>";
                strResult += "    </table>";
                document.getElementById("searchResult").innerHTML = strResult;
            }
        });
    });
}